package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SchoolConsume;
import com.ruoyi.system.api.domain.SchoolPayrecord;
import com.ruoyi.system.api.domain.SysDept;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SchoolPayrecordTj;
import com.ruoyi.system.service.ISchoolConsumeService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.system.service.ISchoolPayrecordService;

import javax.servlet.http.HttpServletResponse;

/**
 * 缴费记录Controller
 * 
 * @author ruoyi
 * @date 2022-11-19
 */
@RestController
@RequestMapping("/payrecord")
public class SchoolPayrecordController extends BaseController
{
    @Autowired
    private ISchoolPayrecordService schoolPayrecordService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISchoolConsumeService schoolConsumeService;

    /**
     * 查询缴费记录列表
     */
    @RequiresPermissions("business:payrecord:list")
    @GetMapping("/list")
    public TableDataInfo list(SchoolPayrecord schoolPayrecord)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolPayrecord.setSchoolId(user.getDeptId());
        }
        startPage();
        List<SchoolPayrecord> list = schoolPayrecordService.selectSchoolPayrecordList(schoolPayrecord);
        return getDataTable(list);
    }

    @GetMapping("/listTj")
    public TableDataInfo listTj(SchoolPayrecord schoolPayrecord)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolPayrecord.setSchoolId(user.getDeptId());
        }
        startPage();
        List<SchoolPayrecordTj> list = schoolPayrecordService.selectSchoolPayrecordListTj(schoolPayrecord);
        return getDataTable(list);
    }

    @GetMapping("/listPayTj")
    public TableDataInfo listPayTj(SchoolPayrecord schoolPayrecord)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolPayrecord.setSchoolId(user.getDeptId());
        }
        startPage();
        List<SchoolPayrecordTj> list = schoolPayrecordService.selectSchoolPayrecordListPayTj(schoolPayrecord);
        return getDataTable(list);
    }

    @GetMapping("/listPayTjPm")
    public TableDataInfo listPayTjPm(SchoolPayrecord schoolPayrecord)
    {
        startPage();
        List<SchoolPayrecordTj> list = schoolPayrecordService.selectSchoolPayrecordListPayTjPm(schoolPayrecord);
        return getDataTable(list);
    }

    /**
     * 导出缴费记录列表
     */
    @RequiresPermissions("business:payrecord:export")
    @Log(title = "缴费记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(HttpServletResponse response, SchoolPayrecord schoolPayrecord)
    {
        SysUser user = SecurityUtils.getLoginUser().getSysUser();
        if (!SysUser.isAdmin(user.getUserId())) {
            schoolPayrecord.setSchoolId(user.getDeptId());
        }
        List<SchoolPayrecord> list = schoolPayrecordService.selectSchoolPayrecordList(schoolPayrecord);
        for (SchoolPayrecord obj:list) {
            SysUser sysUser = sysUserService.selectUserById(obj.getTeacherId());
            SysDept sysDept = sysDeptService.selectDeptById(obj.getSchoolId());
            obj.setSchoolName(sysDept.getDeptName());
            obj.setTeacherUsername(sysUser.getUserName());
            obj.setTeacherPhone(sysUser.getPhonenumber());
        }
        ExcelUtil<SchoolPayrecord> util = new ExcelUtil<SchoolPayrecord>(SchoolPayrecord.class);
        util.exportExcel(response, list, "缴费记录");
    }

    /**
     * 获取缴费记录详细信息
     */
    @RequiresPermissions("business:payrecord:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(schoolPayrecordService.selectSchoolPayrecordById(id));
    }

    /**
     * 新增缴费记录
     */
    @RequiresPermissions("business:payrecord:add")
    @Log(title = "缴费记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SchoolPayrecord schoolPayrecord)
    {
        if (schoolPayrecord.getStudentId()==0) {
            SysUser user = new SysUser();
            user.setPhonenumber(schoolPayrecord.getPhonenumber());
            user.setDeptId(schoolPayrecord.getSchoolId());
            if (UserConstants.NOT_UNIQUE.equals(sysUserService.checkUUserNameUnique(schoolPayrecord.getStudentName(), schoolPayrecord.getSchoolId())))
            {
                return AjaxResult.error("新增用户'" + schoolPayrecord.getStudentName() + "'失败，学生姓名已存在");
            }else if (StringUtils.isNotEmpty(schoolPayrecord.getPhonenumber())
                    && UserConstants.NOT_UNIQUE.equals(sysUserService.checkPhoneUnique(user)))
            {
                return AjaxResult.error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
            }
        }
        return toAjax(schoolPayrecordService.insertSchoolPayrecord(schoolPayrecord));
    }

    /**
     * 修改缴费记录
     */
    @RequiresPermissions("business:payrecord:edit")
    @Log(title = "缴费记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SchoolPayrecord schoolPayrecord)
    {
        return toAjax(schoolPayrecordService.updateSchoolPayrecord(schoolPayrecord));
    }

    /**
     * 删除缴费记录
     */
    @RequiresPermissions("business:payrecord:remove")
    @Log(title = "缴费记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id:ids) {
            SchoolPayrecord schoolPayrecord = schoolPayrecordService.selectSchoolPayrecordById(id);
            SchoolConsume schoolConsume = new SchoolConsume();
            schoolConsume.setSchoolId(schoolPayrecord.getSchoolId());
            schoolConsume.setStudentId(schoolPayrecord.getStudentId());
            List<SchoolConsume> listConsume = schoolConsumeService.selectSchoolConsumeList(schoolConsume);
            if (null!=listConsume&&listConsume.size()>0) {
                return AjaxResult.error("删除缴费记录失败，先确认已存在的耗课记录");
            }
        }
        return toAjax(schoolPayrecordService.deleteSchoolPayrecordByIds(ids));
    }
}
